/*
package thread;

import java.util.ArrayList;
import java.util.List;
import java.util.concurrent.ArrayBlockingQueue;
import java.util.concurrent.BlockingQueue;
import java.util.concurrent.ExecutorService;

*/
/**
 * Created with IntelliJ IDEA.
 * Description:
 * User: Hua YY
 * Date: 2024-09-27
 * Time: 16:48
 *//*



class MyThreadPoolExecutor{
    //2:创建一个顺序表来接收创建的线程
    private List<Thread> threadList = new ArrayList<>();
    //4创建一个容量合适的阻塞队列
    private BlockingQueue<Runnable> queue = new ArrayBlockingQueue(1000);
    //1:通过一个循环，n的值，来控制产生的线程的数量
    public MyThreadPoolExecutor(int n){
        for (int i = 0; i < n; i++) {
            Thread t = new Thread(()->{
                //6:把要做的任务从任务队列中不停地取出来，并且执行
                while(true){
                    try {

                        //带有阻塞的take取出元素
                        Runnable runnable = queue.take();
                        runnable.run();
                    } catch (InterruptedException e) {
                        throw new RuntimeException(e);
                    }
                }

            });
            t.start();
            //3:
            threadList.add(t);
        }
    }
    //5:提交runnable到队列里面去
    public void submit(Runnable runnable) throws InterruptedException {
        queue.put(runnable);
    }
}
public class ThreadDemon35 {
    //大逻辑其实就是，把创建的任务提交上去，再把任务取出来，在run执行就可以了就是这么简单
    public static void main(String[] args) throws InterruptedException {
        MyThreadPoolExecutor executor = new MyThreadPoolExecutor(4);
        for (int i = 0 ; i < 1000 ; i++){
            //变量捕获
            int n = i;
            executor.submit(new Runnable() {
                @Override
                public void run() {
                    System.out.println("执行任务：" + n + "  " + "当前线程为：" + Thread.currentThread().getName());
                }
            });
        }
    }
}
*/
